/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package syspizza.DAO;

import syspizza.DAO.IDAO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import syspizza.sabor.Sabor;

/**
 *
 * @author André
 */
public class SaborDAO implements IDAO<Sabor> {

    //DEFINIR CAMINHO DOS ARQUIVOS     
    private final String PASTA = "\\SysPizza\\dados\\sabor\\";
    private final String ARQUIVO = "sabores.dat";

    /**
     *
     * @param sabor
     * @return
     * @throws Exception
     */
    @Override
    public boolean insert(Sabor sabor) {
        verificaPasta();
        List<Sabor> lista = getAll();
        int codigo = getNextId();
        sabor.setId(codigo);
        lista.add(sabor);
        try {
            write(lista);
        } catch (IOException ex) {
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(int id) {
        List<Sabor> sabores = getAll();
        boolean removido = sabores.removeIf(s -> s.getId() == id);
        if (removido) {
            try {
                write(sabores);
            } catch (IOException ex) {
                return false;
            }
        }
        return removido;
    }

    @Override
    public boolean update(Sabor sab) {
        List<Sabor> sabores = getAll();
        Sabor p = null;
        for (Sabor sabor : sabores) {
            if (sabor.getId() == sab.getId()) {
                p = sabor;
            }
        }

        if (p != null) {
            sabores.remove(p);
            sabores.add(sab);
            try {
                write(sabores);
                return true;
            } catch (IOException ex) {
                return false;
            }
        }
        return false;
    }

    @Override
    public List<Sabor> getAll() {
        //pega a lista com todos os registros
        List<Sabor> sabores = new ArrayList<>();
        verificaPasta();
        File[] arqs = new File(PASTA).listFiles();

        for (File file : arqs) {
            if (file.isFile()) {
                try {
                    FileInputStream fis = new FileInputStream(file);
                    ObjectInputStream ois = new ObjectInputStream(fis);
                    sabores = (ArrayList<Sabor>) ois.readObject();

                    ois.close();
                    fis.close();
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(SaborDAO.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException | ClassNotFoundException ex) {
                    Logger.getLogger(SaborDAO.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
        return sabores;
    }

    @Override
    public Sabor getById(int id) {
        List<Sabor> sabores = getAll();
        for (Sabor sabor : sabores) {
            if (sabor.getId() == id) {
                return sabor;
            }
        }
        return null;
    }

    @Override
    public int getNextId() {
        int id = 0;
        verificaPasta();
        for (Sabor sabor : getAll()) {
            if (sabor.getId() > id) {
                id = sabor.getId();
            }
        }
        id++;
        return id;
    }

    private boolean verificaPasta() {
        File arq = new File(PASTA);

        if (!arq.exists()) {
            arq.mkdirs();
        }
        return true;
    }

    public void write(List<Sabor> sabores) throws FileNotFoundException, IOException {
        verificaPasta();
        File arq = new File(PASTA + ARQUIVO);
        try (FileOutputStream fos = new FileOutputStream(arq);
                ObjectOutputStream oos = new ObjectOutputStream(fos)) {
            oos.writeObject(sabores);
        }
    }
    public List<Sabor> getAll(String nome){
        List<Sabor> sabores = getAll();
        
        if (!(nome.isEmpty()||nome == null)){
            sabores.removeIf(p -> !p.getNome().toUpperCase().contains(nome.toUpperCase()));
        }
        return sabores;        
    }
    
    

}
